package BeenChina.PM.Action;

import BeenChina.PM.Model.ModelUser;
import BeenChina.PM.Service.ServiceFactory;
import BeenChina.PM.Util.CookieUtil;
import BeenChina.PM.Util.SecurityUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class ChangePassWordAction  extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String msg = "";
	
	public String getMsg() {
		return msg;
	}
	
	private String oldPassWord,newPassWord,confirmPassWord;

	 
	public void setOldPassWord(String oldPassWord) {
		this.oldPassWord = oldPassWord;
	}
 

	public void setNewPassWord(String newPassWord) {
		this.newPassWord = newPassWord;
	}
 

	public void setConfirmPassWord(String confirmPassWord) {
		this.confirmPassWord = confirmPassWord;
	}
 
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub

		
		ModelUser user = (ModelUser) ActionContext.getContext().getSession().get("user");
		if (user!=null)
		{
			boolean flag = SecurityUtil.authenticatePassword(user.getUserPass(), oldPassWord);
			
			if (flag)
			{
				  if (!newPassWord.equals(confirmPassWord)){
						return ActionSupport.ERROR;
					}
					else {
					
						ServiceFactory.getInstance().getSystemInfoService().changePassWord(user.getUserID(), newPassWord);
						
						//change password
						user.setUserPass(newPassWord);
						
						//set cookie
						//ActionContext context = ActionContext.getContext();	
						//context.getSession().put("user", user);		
						
						//update cookie 
						if (CookieUtil.isSaveCookie())
						{
							CookieUtil.delCookie();
							CookieUtil.setCookie(user);
						}
						
						msg = "パスワードは改正することに成功します";
						
						return ActionSupport.SUCCESS;
					}
				
			}
			else {
				msg = "古いパスワードが違います";
				return ActionSupport.ERROR;
			}
		}
		return ActionSupport.ERROR;	
		 
	}




}
